package com.kifas.controller;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.kifas.entity.AkunMasterEntity;
import com.kifas.entity.BukuVoucherEntity;
import com.kifas.entity.TypeAkunEntity;
import com.kifas.model.xml.AkunMasterModel;
import com.kifas.model.xml.BukuVoucherModel;
import com.kifas.model.xml.PersistStatus;
import com.kifas.model.xml.TypeAkunModel;
import com.kifas.service.EntityServiceManager;

@Component
@Scope("prototype")
@Path("/BukuVouchers")
public class BukuVoucherController {

	private static Logger log = Logger.getLogger(BukuVoucherController.class);

	@Context
	UriInfo uriInfo;

	@Autowired
	private EntityServiceManager entityManager;

	@GET
	@Produces(MediaType.TEXT_XML)
	public List<BukuVoucherModel> getBukuVouchers() {
		return transformList(entityManager.getBukuVouchers());
	}

	@GET
	@Path("{id}")
	@Produces(MediaType.TEXT_XML)
	public BukuVoucherModel getBukuVoucher(@PathParam("id") Long idBukuVoucher) {
		return transform(entityManager.getBukuVoucher(idBukuVoucher));
	}

	@POST
	@Produces(MediaType.APPLICATION_XML)
	@Consumes(MediaType.APPLICATION_XML)
	public PersistStatus insert(JAXBElement<BukuVoucherModel> xml) {
		BukuVoucherModel model = xml.getValue();
		AkunMasterEntity akunMaster = entityManager.getAkunMaster(model.getAkunMaster().getKodeAkun());
		BukuVoucherEntity bukuVoucher = new BukuVoucherEntity(null, akunMaster, model.getDebet(), model.getKredit(), model.getTanggal(), model.getKeterangan(),
				model.getPosting(), model.getNoRef(), model.getDeskripsi(), model.getCreatedBy(), model.getCreatedDate());
		try {
			entityManager.persist(bukuVoucher);
		} catch (Exception e) {
			log.error("can not persist buku voucher entity", e);
			return new PersistStatus("BukuVoucherEntity", "failed", "can not persist buku voucher entity");
		}
		return new PersistStatus("BukuVoucherEntity", "success");
	}

	@PUT
	@Produces(MediaType.APPLICATION_XML)
	@Consumes(MediaType.APPLICATION_XML)
	public PersistStatus update(JAXBElement<BukuVoucherModel> xml) {
		BukuVoucherModel model = xml.getValue();
		BukuVoucherEntity bukuVoucher = entityManager.getBukuVoucher(model.getIdBukuVoucher());
		AkunMasterEntity akunMaster = entityManager.getAkunMaster(model.getAkunMaster().getKodeAkun());
		bukuVoucher.setAkunMaster(akunMaster);
		bukuVoucher.setDebet(model.getDebet());
		bukuVoucher.setKredit(model.getKredit());
		bukuVoucher.setKeterangan(model.getKeterangan());
		bukuVoucher.setDeskripsi(model.getDeskripsi());
		bukuVoucher.setNoRef(model.getNoRef());
		bukuVoucher.setPosting(model.getPosting());
		bukuVoucher.setTanggal(model.getTanggal());
		try {
			entityManager.update(bukuVoucher);
		} catch (Exception e) {
			log.error("can not update buku voucher entity", e);
			return new PersistStatus("BukuVoucherEntity", "failed", "can not update buku voucher entity");
		}
		return new PersistStatus("BukuVoucherEntity", "success");
	}

	private List<BukuVoucherModel> transformList(List<BukuVoucherEntity> bukuVouchers) {
		List<BukuVoucherModel> models = new ArrayList<BukuVoucherModel>();
		for (BukuVoucherEntity bukuVoucher : bukuVouchers) {
			models.add(transform(bukuVoucher));
		}
		return models;
	}

	private BukuVoucherModel transform(BukuVoucherEntity bukuVoucher) {
		AkunMasterEntity akunMasterEntity = bukuVoucher.getAkunMaster();
		TypeAkunEntity typeAkunEntity = akunMasterEntity.getTypeAkun();
		TypeAkunModel typeAkunModel = new TypeAkunModel(typeAkunEntity.getIdType(), typeAkunEntity.getNama(), typeAkunEntity.getDeskripsi(),
				typeAkunEntity.getKode(), typeAkunEntity.getCreatedBy());
		AkunMasterModel akunMaster = new AkunMasterModel(akunMasterEntity.getKodeAkun(), akunMasterEntity.getNama(), akunMasterEntity.getDk(),
				akunMasterEntity.getIdParentAkun(), akunMasterEntity.isActive(), akunMasterEntity.getDeskripsi(), akunMasterEntity.getNamaPanjang(),
				typeAkunModel, akunMasterEntity.getCreatedBy());
		return new BukuVoucherModel(bukuVoucher.getIdBukuVoucher(), akunMaster, bukuVoucher.getDebet(), bukuVoucher.getKredit(), bukuVoucher.getTanggal(),
				bukuVoucher.getKeterangan(), bukuVoucher.getPosting(), bukuVoucher.getNoRef(), bukuVoucher.getDeskripsi(), bukuVoucher.getCreatedBy(),
				bukuVoucher.getCreatedDate());
	}

}
